<!--  -->
<template>
  <div class="video flex column grow">
    <div class="box border flex column grow">
      <div class="head shrink">
        <span class="back" @click="goBack"><i class="icon"></i>返回</span>
        <span class="title">{{ title }}</span>
      </div>
      <div class="tag shrink clear" v-show="types.length > 0">
        <span
          class="item fl"
          :class="{ cur: videoConfigId == '' }"
          @click="taggle('')"
          >全部</span
        >
        <span
          class="item fl"
          v-for="item in types"
          :class="{ cur: videoConfigId == item.id }"
          :key="item.id"
          @click="taggle(item.id)"
          >{{ item.channelName }}</span
        >
      </div>
      <img
        src="../images/living2.png"
        class="liveBanner flex shrink"
        v-show="bannerShow"
        alt=""
      />
      <iframe
        class="player flex shrink"
        :src="videoUrl"
        v-show="videoUrl"
        frameborder="0"
      ></iframe>
      <div class="videoList flex grow">
        <div class="list clear grow">
          <div
            class="item fl"
            v-for="item in videoList"
            :key="item.videoId"
            @click="
              openPoup(
                path + '/client/recordLiveOff.htm?recordId=' + item.recordId,
                item.subject
              )
            "
          >
            <div class="whole">
              <img :src="item.wholeImage" alt="" />
            </div>
            <p class="title">{{ item.subject }}</p>
            <p class="time">{{ item.time }}</p>
          </div>
        </div>
      </div>
    </div>
    <div class="timeTable" v-show="recordList.length > 0">
      <div class="title"></div>
      <div class="list">
        <div class="item" v-for="item in recordList" :key="item.configId">
          <p class="time">{{ item.time }}</p>
          <p class="channelName">{{ item.channelName }}</p>
        </div>
      </div>
    </div>
    <div class="flex shrink column">
      <m-page
        @onPage="onPage"
        :pageSize="params.pageSize"
        :currentPage="params.currentPage"
        :total="total"
      ></m-page>
    </div>
  </div>
</template>

<script>
import mPage from "../component/mPage";
import {
  getNoticeList,
  queryVideoConfig,
  queryRecordChargeLiveListByWeight,
  queryRecordChargeLiveList,
} from "../api/index";
import dayjs from "dayjs";
import { openWeb } from "../script/utils";
import { CLIENT } from "@/common/const";

export default {
  data() {
    return {
      path: CLIENT,
      productId: "",
      videoConfigId: "",
      title: "",
      token: "",
      videoUrl: "",
      total: 0,
      bannerShow: false,
      types: [],
      videoList: [],
      recordList: [],
      timer: "",
      params: {
        pageSize: 20,
        currentPage: 1,
        recordType: 0,
      },
    };
  },
  beforeRouteEnter(to, from, next) {
    const { productId, videoConfigId, title, token } = to.params;
    next((vm) => {
      vm.productId = productId;
      vm.videoConfigId = videoConfigId;
      vm.token = token;
      vm.types = [];
      vm.title = "";
      vm.videoUrl = "";
      vm.total = 0;
      vm.params.recordType = 0;
      if (title == "往期课程") {
        vm.videoConfigId = "";
        vm.title = title;
        vm.queryVideoConfig();
        vm.getNoticeList();
      }
      if (title == "精品视频") {
        vm.videoConfigId = "";
        // vm.title = title;
        vm.params.recordType = 1;
        vm.getWeightVideoList();
      } else {
        vm.getVideoList();
      }
    });
  },
  components: { "m-page": mPage },

  computed: {},

  mounted() {},

  methods: {
    openPoup(url, title, type = 1) {
      try {
        console.log(url);
        openWeb(url, title, type);
      } catch (error) {
        console.log(error);
      }
    },
    goBack() {
      this.$router.go(-1);
    },
    taggle(id) {
      this.videoConfigId = id;
      this.params.currentPage = 1;
      this.getVideoList();
    },
    async getNoticeList() {
      const beginTimeStr = dayjs(Date.now()).format("YYYY-MM-DD");
      const endTimeStr = dayjs().add(7, "day").format("YYYY-MM-DD");
      await getNoticeList({
        productId: this.productId,
        beginTimeStr,
        endTimeStr,
      }).then((back) => {
        if (back.status == 1) {
          let data = back.data;
          const { beginTime, endTime } = data[0];
          //   if (now > beginTime && beginTime < endTime) {
          //     this.videoUrl = `http://yncytz.gensee.com/webcast/site/entry/join-${id}?nickName=用户${now}`;
          //     this.params.pageSize = 5;
          //     this.getVideoList();
          //   }
          if (data.length > 0) {
            this.timer = window.requestAnimationFrame(this.timerDevice);
          }
          this.recordList = data.map((item) => {
            item.time = dayjs(item.beginTimeStr).format("MM-DD HH:mm");
            return item;
          });
        }
      });
    },
    timerDevice() {
      if (this.recordList.length > 0) {
        const now = Date.now();
        const { beginTime, endTime, channelId } = this.recordList[0];
        const fMin = 1000 * 60 * 5;
        if (beginTime > now && beginTime - now < fMin) {
          this.bannerShow = true;
        }
        if (now > beginTime && now < endTime) {
          if (!this.videoUrl) {
            this.videoUrl = `http://yncytz.gensee.com/webcast/site/entry/join-${channelId}?nickName=用户${now}`;
            this.params.pageSize = 5;
            this.getVideoList();
          }
          this.bannerShow = false;
          this.timer = window.requestAnimationFrame(this.timerDevice);
        }
        if (now > endTime + 1000 * 60 * 30) {
          this.videoUrl = "";
          this.params.pageSize = 20;
          this.getVideoList();
          window.cancelAnimationFrame(this.timer);
        }
        if (now < beginTime) {
          this.timer = window.requestAnimationFrame(this.timerDevice);
        }
      }
    },
    async queryVideoConfig() {
      await queryVideoConfig({ productId: this.productId }).then((back) => {
        if (back.status == 1) {
          let data = back.data;
          const types = data.map((item) => ({
            channelName: item.channelName,
            productId: item.productId,
            id: item.id,
          }));
          this.types = types;
        }
      });
    },
    async getWeightVideoList() {
      await queryRecordChargeLiveListByWeight({
        videoConfigId: this.videoConfigId,
        productId: this.productId,
        clientCentralToken: this.token,
        ...this.params,
      }).then((back) => {
        if (back.status == 1) {
          let videos = back.data.videos;
          let total = back.data.total;
          this.total = total;
          //   if (this.title == "精品视频") {
          videos = videos
            .filter((item) => item.recordedVideoWeight > 0)
            .sort((a, b) => b.recordedVideoWeight - a.recordedVideoWeight);
          //   }
          this.videoList = videos.map((item) => {
            item.time =
              dayjs(item.recordStartTime).format("MM-DD HH:mm-") +
              dayjs(item.recordEndTime).format("HH:mm");
            return item;
          });
        }
      });
    },
    async getVideoList() {
      let { videos, total } = await queryRecordChargeLiveList({
        videoConfigId: this.videoConfigId,
        productId: this.productId,
        clientCentralToken: this.token,
        ...this.params,
      }).then((back) => {
        if (back.status == 1) {
          let videos = back.data.videos;
          let total = back.data.total;
          this.total = total;
          //   if (this.title == "精品视频") {
          //     videos = videos.filter((item) => item.recordedVideoWeight > 0);
          //   }
          this.videoList = videos.map((item) => {
            item.time =
              dayjs(item.recordStartTime).format("MM-DD HH:mm-") +
              dayjs(item.recordEndTime).format("HH:mm");
            return item;
          });
        }
      });
    },
    onPage(page) {
      this.params.currentPage = page;
      const { title } = this.$route.params;
      if (title == "精品视频") {
        this.getWeightVideoList();
      } else {
        this.getVideoList();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.border {
  border: 1px solid #27272d;
}
.video {
  width: 1200px;
  margin: 10px auto 20px;
  position: relative;
  .box {
    border-radius: 6px;
    .head {
      height: 34px;
      background: #27272d;
      position: relative;
      line-height: 34px;
      text-align: center;
      .back {
        color: #a9adb1;
        font-size: 14px;
        padding-left: 14px;
        position: absolute;
        left: 0;
        cursor: pointer;
        .icon {
          display: inline-block;
          width: 8px;
          height: 12px;
          background: url(../images/back.png) no-repeat center;
          background-size: 100%;
          margin-right: 8px;
        }
      }
      .title {
        color: #ff4e39;
        font-size: 18px;
      }
    }
    .tag {
      background: #27282e;
      border: 1px solid #090910;
      line-height: 28px;
      .item {
        background: #27282e;
        border-right: 1px solid #090910;
        font-size: 14px;
        width: 79px;
        cursor: pointer;
        text-align: center;
        color: #bcbfc4;
        &:hover,
        &.cur {
          background: #35497c;
          color: #ffffff;
        }
      }
    }
    .liveBanner {
      height: 476px;
    }
    .player {
      height: 476px;
    }
    .videoList {
      .list {
        overflow: auto;
        padding: 0 7px;
        .item {
          background: #1d202c;
          padding: 9px;
          margin: 16px 6px;
          border-radius: 4px;
          cursor: pointer;
          .whole {
            width: 204px;
            height: 139px;
            overflow: hidden;
            margin-bottom: 8px;
            img {
              width: 100%;
              display: block;
            }
          }
          .title {
            font-size: 16px;
            line-height: 26px;
            color: #dddddd;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            max-width: 204px;
          }
          .time {
            font-size: 14px;
            color: #bcbfc4;
            padding: 4px 0;
          }
        }
      }
    }
  }
  .timeTable {
    position: absolute;
    width: 166px;
    height: 554px;
    top: 0;
    right: -181px;
    background: url(../images/bg.png) no-repeat center;
    padding-top: 27px;
    box-sizing: border-box;
    .title {
      height: 39px;
      background: url(../images/kcb2.png) no-repeat center;
      margin-bottom: 11px;
    }
    .list {
      height: 428px;
      background: url(../images/biaoge.png) no-repeat center;
      .item {
        text-align: center;
        font-size: 18px;
        color: #ffffff;
        padding-top: 4px;
        line-height: 26px;
        box-sizing: border-box;
        height: 61px;
      }
    }
  }
}
</style>
